var t = getApp();

Page({
    data: {
        isProve: !1,
        pageMode: null,
        loading: !0,
        showCode: !1,
        finish: !1,
        status: 0,
        field: {},
        code_url: "",
        dataId: "",
        timer: null,
        thumb: null,
        description: null
    },
    onLoad: function(o) {
        var a = null;
        if (o.scene) {
            var e = decodeURIComponent(o.scene);
            if (e) {
                var i = [];
                i = e.split(",");
                for (var s = 0; s < i.length; s++) {
                    var n;
                    "id" == (n = i[s].split("="))[0] ? this.data.id = n[1] : "mid" == n[0] ? t._setMid(n[1]) : "mode" == n[0] && (a = n[1]);
                }
            }
        } else a = this.options.mode, this.data.id = this.options.id;
        "submit" == a ? this.getData() : "prove" == a && (this.setData({
            pageMode: "prove"
        }), this.getProveData());
    },
    getData: function() {
        var o = this;
        if (this.options.id) {
            var a = t.getNetAddresss("plugin.diy-form-prove.frontend.controllers.prove.formDetail");
            t._postNetWork({
                url: a,
                data: {
                    id: this.data.id
                },
                success: function(t) {
                    var a = t.data;
                    1 == a.result ? o.setData({
                        thumb: a.data.form_data.thumb || null,
                        description: a.data.form_data.description || null,
                        loading: !1,
                        field: a.data.form_data.fields,
                        status: a.data.form_data.status
                    }) : wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1e3
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    submit: function(o) {
        var a = this;
        console.log(o.detail);
        var e = t.getNetAddresss("plugin.diy-form-prove.frontend.controllers.prove.saveData");
        t._postNetWork({
            url: e,
            data: {
                form_id: this.data.id,
                form_data: o.detail
            },
            success: function(t) {
                var o = t.data;
                1 == o.result ? (a.data.dataId = o.data.id, a.getEwmImg(o.data.id)) : wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getEwmImg: function(o) {
        var a = this, e = t.getNetAddresss("plugin.diy-form-prove.frontend.controllers.prove.proveCode");
        t._postNetWork({
            url: e,
            data: {
                data_id: o
            },
            success: function(t) {
                var o = t.data;
                1 == o.result ? (a.setData({
                    code_url: "".concat(o.data.code_url, "?").concat(Date.now()),
                    showCode: !0,
                    finish: !1
                }), a.pollTimers(), a.selectComponent(".control-count-down").reset()) : wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    pollTimers: function() {
        var o = this;
        if (!this.data.finish) {
            var a = this, e = t.getNetAddresss("plugin.diy-form-prove.frontend.controllers.prove.checkProve");
            t._postNetWork({
                url: e,
                data: {
                    id: this.data.dataId
                },
                success: function(t) {
                    var e = t.data;
                    1 == e.result ? 0 == e.data.status ? o.data.timer = setTimeout(function() {
                        a.pollTimers();
                    }, 3e3) : (wx.showToast({
                        icon: "none",
                        title: "核销成功",
                        duration: 1e3
                    }), o.setData({
                        status: 1
                    }), o.initForm()) : wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1e3
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    close: function() {
        var t = this, o = this.data.finish ? "二维码链接已失效，请重新提交" : "关闭后数据需要重新填写，是否确认关闭";
        wx.showModal({
            title: "提示",
            content: o,
            success: function(o) {
                o.confirm ? (t.delForm(), t.initForm()) : o.cancel && console.log("用户点击取消");
            }
        });
    },
    delForm: function() {
        var o = this, a = t.getNetAddresss("plugin.diy-form-prove.frontend.controllers.prove.delProve");
        t._postNetWork({
            url: a,
            data: {
                data_id: this.data.dataId
            },
            success: function(t) {
                var a = t.data;
                1 == a.result ? o.getData() : wx.showToast({
                    icon: "none",
                    title: a.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    finishTimer: function() {
        this.setData({
            finish: !0
        }), this.delForm(), this.initForm();
    },
    initForm: function() {
        this.data.timer && clearTimeout(this.data.timer), this.setData({
            showCode: !1,
            code_url: "",
            dataId: null
        });
    },
    getProveData: function() {
        var o = this, a = t.getNetAddresss("plugin.diy-form-prove.frontend.controllers.prove.dataDetail");
        t._postNetWork({
            url: a,
            data: {
                id: this.data.id
            },
            success: function(t) {
                var a = t.data;
                1 == a.result ? o.setData({
                    field: a.data.form_data.fields,
                    status: 1,
                    loading: !1,
                    isProve: a.data.form_data.is_prove || !1,
                    thumb: a.data.form_data.thumb || null,
                    description: a.data.form_data.description || null
                }) : wx.showModal({
                    title: "提示",
                    content: a.msg,
                    success: function(t) {
                        t.confirm ? console.log("用户点击确定") : t.cancel && console.log("用户点击取消");
                    }
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    proveForm: function() {
        var o = this;
        if (!this.data.isProve) {
            var a = t.getNetAddresss("plugin.diy-form-prove.frontend.controllers.prove.submitProve");
            t._postNetWork({
                url: a,
                data: {
                    data_id: this.data.id
                },
                success: function(t) {
                    var a = t.data;
                    wx.showModal({
                        title: "提示",
                        content: a.msg,
                        success: function(t) {}
                    }), 1 == a.result && o.setData({
                        isProve: !0
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {
        this.data.timer && clearTimeout(this.data.timer);
    },
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: this.data.thumb,
            imageUrl: this.data.description
        };
    }
});